﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JOthello.Forms
{
    public partial class WaitForm : Form
    {
        private Timer t;

        private string _baseText = "";
        public string baseText
        {
            get
            {
                return _baseText;
            }
            set
            {
                _baseText = value;
                lblThinking.Text = _baseText + "...";
            }
        }

        private bool baseTextEqualTitel;

        public WaitForm(string text, string titel = null, bool canCancel = false)
        {
            InitializeComponent();

            baseText = text;

            baseTextEqualTitel = titel == null;

            if (titel != null) this.Text = titel;

            this.btnCancel.Visible = this.btnCancel.Enabled = canCancel;

            this.CancelButton = btnCancel;
            btnCancel.Click += (s, e) => { this.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.Close(); };

            #region timer
            t = new Timer();
            t.Interval = 10;
            t.Tick += (s, e) =>
            {
                switch (DateTime.Now.Second % 4)
                {
                    case 0:
                        lblThinking.Text = baseText;
                        break;
                    case 1:
                        lblThinking.Text = baseText + ".";
                        break;
                    case 2:
                        lblThinking.Text = baseText + "..";
                        break;
                    case 3:
                        lblThinking.Text = baseText + "...";
                        break;
                }
                if (baseTextEqualTitel) this.Text = lblThinking.Text;
            };

            this.Load += (s, e) => t.Start();
            this.Disposed += (s, e) => t.Stop();
            this.Closed += (s, e) => t.Stop();
            #endregion
        }

        public void finish()
        {
            if (this.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                });
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
